package com.safety51.bi.controller;

import com.safety51.bi.entity.ApiResponse;
import com.safety51.bi.entity.ChartConfig;
import com.safety51.bi.entity.ChartDataRequest;
import com.safety51.bi.entity.NumberMetricData;
import com.safety51.bi.service.ChartConfigService;
import com.safety51.bi.service.ChartDataService;
import com.safety51.bi.service.NumberMetricService;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/bi")
@CrossOrigin(origins = "*")
public class BiChartController {

    private final ChartDataService chartDataService;
    private final NumberMetricService numberMetricService;
    private final ChartConfigService chartConfigService;

    public BiChartController(ChartDataService chartDataService,
                             NumberMetricService numberMetricService,
                             ChartConfigService chartConfigService) {
        this.chartDataService = chartDataService;
        this.numberMetricService = numberMetricService;
        this.chartConfigService = chartConfigService;
    }

    @PostMapping("/data")
    public ApiResponse<?> getData(@RequestBody ChartDataRequest request) {
        try {
            ChartConfig config = chartConfigService.getConfigByChartId(request.getChartId());
            if (config == null) {
                return ApiResponse.error("配置不存在: " + request.getChartId());
            }

            String displayType = config.getDisplayType();
            if (displayType == null) {
                displayType = "chart";
            }

            switch (displayType) {
                case "number":
                    return numberMetricService.getNumberMetric(request);
                case "table":
                    Object tableData = chartDataService.getTableData(request);
                    return ApiResponse.success(tableData);
                case "chart":
                default:
                    Object chartData = chartDataService.getChartData(request);
                    return ApiResponse.success(chartData);
            }
        } catch (Exception e) {
            return ApiResponse.error("获取数据失败: " + e.getMessage());
        }
    }

    @PostMapping("/batch-numbers")
    public ApiResponse<Map<String, NumberMetricData>> getBatchNumberMetrics(
            @RequestBody Map<String, Object> request) {

        @SuppressWarnings("unchecked")
        List<String> chartIds = (List<String>) request.get("chartIds");
        @SuppressWarnings("unchecked")
        Map<String, Object> filters = (Map<String, Object>) request.get("filters");

        if (chartIds == null || chartIds.isEmpty()) {
            return ApiResponse.error("chartIds不能为空");
        }

        return numberMetricService.getBatchNumberMetrics(chartIds, filters);
    }

    @GetMapping("/configs")
    public ApiResponse<List<ChartConfig>> getAllConfigs() {
        try {
            List<ChartConfig> configs = chartConfigService.getAllActiveConfigs();
            return ApiResponse.success(configs);
        } catch (Exception e) {
            return ApiResponse.error("获取配置失败: " + e.getMessage());
        }
    }
}